#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/time.h>
#include <linux/rtc.h>
#include <asm/uaccess.h>
#define HELLO_MAJOR 200
#define DEVICE_NAME "Hello"

static int hello_open(struct inode *inode, struct file *file)
{
  printk("Opening files or devices\n");
  return 0;
}
static int hello_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
{
	printk("Writing in buffer\n");
	int x;
	get_user(x,buf);
	if(x==0 || x==1){
	printk("You wrote %d\n",x);
	}
	else{
	printk("Please write 0 or 1\n");
	}
//	int ret=0;
//	char *desp;
//	ret=copy_from_user(desp,buf,count);
//	if(desp==0){
//		printk("You wrote 0\n");
//	}
//	else if(desp==1){
//		printk("You wrote 1\n");
//	}
//	else{
//		printk("Please input 0 or 1\n");
//	}
        return 0;
}
static int hello_read(struct file *file,const char __user *buf,size_t count, loff_t *ppos)
{
    struct timeval now=
    {
        .tv_sec=0,
        .tv_usec=0
    }; 
    struct rtc_time tm;
    do_gettimeofday(&now);
    rtc_time_to_tm(now.tv_sec, &tm);
    printk("Current time is %d-%02d-%02d %02d:%02d:%02d\n",tm.tm_year + 1900, tm.tm_mon +1, tm.tm_mday,tm.tm_hour +8, tm.tm_min, tm.tm_sec);
    return 0;
}
static struct file_operations hello_flops =
{
  .owner = THIS_MODULE,
  .open = hello_open,
  .write = hello_write,
  .read = hello_read,
};
static int hello_init(void)
{
  int ret;
  ret=register_chrdev(HELLO_MAJOR,DEVICE_NAME,&hello_flops);
   if(ret<0){
	printk("Can`t register major number.\n");
	return ret;
}
  printk("hello\n");
  return 0;
}
static void hello_exit(void)
{
  unregister_chrdev(HELLO_MAJOR, DEVICE_NAME);
      printk("exit\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
